<?php
/* 	These code was generated using phpCIGen v 2.a (21/05/2012)
	#zaqi 		zaqi.smart@gmail.com,http://zenzaqi.blogspot.com, 
	#CV. Trust Solution, jl. Saronojiwo 2/19 Surabaya, http://www.ts.co.id
	
	+ Module  		: t_rapk Controller
	+ Description	: For record controller process back-end
	+ Filename 		: C_t_rapk.php
 	+ creator 		: 
 	+ Created on 24/Jul/2012 10:52:52
	
*/

//class of t_rapk
class C_t_rapk extends CI_Controller {

	//constructor
	function __construct(){
		parent::__construct();
		
		$this->load->model('m_t_rapk', '', TRUE);
		checkLogin();
	}
	
	function get_rapktotal(){

		$by = @$_SESSION[SESSION_USERID];
		echo '({"total":"0", "results":'.json_encode($this->m_t_rapk->get_rapktotal($by)).'})';
	}
	
	function rapkdet_list(){
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$master_id = (integer) (isset($_POST['master_id']) ? @$_POST['master_id'] : @$_GET['master_id']);
		$task = isset($_POST['task']) ? @$_POST['task'] : @$_GET['task'];
		$result=$this->m_t_rapk->rapkdet_list($task,$master_id,$query,$start,$end);
		echo $result;
	}
	
	function get_parentname_byrapk(){
		$master_id = (integer) (isset($_POST['master_id']) ? @$_POST['master_id'] : @$_GET['master_id']);
		$result=$this->m_t_rapk->get_parentname_byrapk($master_id);
		echo $result;
	}
	
	function get_group_byrapk(){
		$master_id = (integer) (isset($_POST['master_id']) ? @$_POST['master_id'] : @$_GET['master_id']);
		$result=$this->m_t_rapk->get_group_byrapk($master_id);
		echo $result;
	}
	
	function t_rapkdet_grouptemp_getAll(){
		$by = @$_SESSION[SESSION_USERID];
		$result=$this->m_t_rapk->t_rapkdet_grouptemp_getAll($by);
		echo $result;
	}
	
	function t_rapkdet_groupdettemp_getAll(){
		$group_id=@$_POST["group_id"];
		$group_id=is_numeric($group_id)?$group_id:0;
		$result=$this->m_t_rapk->t_rapkdet_groupdettemp_getAll($group_id);
		echo $result;
	}
	
	function t_rapkdet_groupdettemp_getByGroup(){
		$group = (integer) (isset($_POST['group']) ? @$_POST['group'] : @$_GET['group']);
		$result=$this->m_t_rapk->t_rapkdet_groupdettemp_getByGroup($group);
		echo $result;
	}
	
	//set index
	function index(){
		$this->load->view('main/v_t_rapk');
	}
	
	//event handler action
	function get_action(){
		$task = $_POST['task'];
		switch($task){
			case "LIST":
				$this->t_rapk_list();
				break;
			case "UPDATE":
				$this->t_rapk_update();
				break;
			case "CREATE":
				$this->t_rapk_create();
				break;
			case "DELETE":
				$this->t_rapk_delete();
				break;
			case "SEARCH":
				$this->t_rapk_search();
				break;
			case "PRINT":
				$this->t_rapk_print();
				break;
			case "EXCEL":
				$this->t_rapk_export_excel();
				break;
			case "EDIT_SUBKON":
				$this->t_rapk_edit_subkon();
				break;
			default:
				echo "{failure:true}";
				break;
		}
	}
	
	function t_rapk_edit_subkon(){
		$by = @$_SESSION[SESSION_USERID];
		$this->m_t_rapk->t_rapkdet_grouptemp_truncate($by);
		
		$rapk_id=@$_POST["rapk_id"];
		$rapk_id=is_numeric($rapk_id)?$rapk_id:0;
		$by = @$_SESSION[SESSION_USERID];
		echo $this->m_t_rapk->t_rapk_edit_subkon($rapk_id,$by);
	}
	
	//function fot list record
	function t_rapk_list(){
		
		$query = isset($_POST['query']) ? @$_POST['query'] : "";
		$start = (integer) (isset($_POST['start']) ? @$_POST['start'] : @$_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? @$_POST['limit'] : @$_GET['limit']);
		$result=$this->m_t_rapk->t_rapk_list($query,$start,$end);
		echo $result;
	}
	
	//function for create new record
	function t_rapk_create(){
		$div_name=@$_POST["div_id"];
		//POST varible here
		//auto increment, don't accept anything from form values
		/*$array_object=@$_POST["object"];
		$array_object=json_decode(stripslashes($array_object));
		$array_parentcheck=@$_POST["parentcheck"];
		$array_parentcheck=json_decode(stripslashes($array_parentcheck));
		$this->firephp->log($array_object);
		$arr = $array_object;
		$i = 0;
		foreach ($arr as $rows){
			$this->firephp->log(key($rows));
			$this->firephp->log($array_parentcheck[$i]);
			//$this->firephp->log($row);
			//foreach ($rows as $row){
			//	$this->firephp->log($row->jobdet_id);
			//}
			$i++;
		}*/
		
		
		$branch=@$_POST["branch"];
		$branch=is_numeric($branch)?$branch:0;
		$project=trim(@$_POST["project"]);
		$project=htmlentities($project,ENT_QUOTES);
		$job=trim(@$_POST["job"]);
		$job=htmlentities($job,ENT_QUOTES);
		$procurement=trim(@$_POST["procurement"]);
		$procurement=htmlentities($procurement,ENT_QUOTES);
		$rapk_total=@$_POST["rapk_total"];
		$rapk_total=htmlentities($rapk_total);
		$div_id = $this->m_public_function->getDivIdByName($div_name);
		if ($div_name != '503. Sub Kontraktor'){
			//data detail --> supplier
			$array_jobdet_id=@$_POST["jobdet_id"];
			$array_jobdet_id=json_decode(stripslashes($array_jobdet_id));
			$array_jobdet_name=@$_POST["jobdet_name"];
			$array_jobdet_name=json_decode(stripslashes($array_jobdet_name));
			$array_rapkdet_volume=@$_POST["rapkdet_volume"];
			$array_rapkdet_volume=json_decode(stripslashes($array_rapkdet_volume));
			$array_rapkdet_unit=@$_POST["rapkdet_unit"];
			$array_rapkdet_unit=json_decode(stripslashes($array_rapkdet_unit));
			$array_rapkdet_price=@$_POST["rapkdet_price"];
			$array_rapkdet_price=json_decode(stripslashes($array_rapkdet_price));
			$result=$this->m_t_rapk->t_rapk_create_sup($rapk_total,$procurement,$div_id ,$branch, $project, $job, $array_jobdet_id, $array_jobdet_name, $array_rapkdet_volume,
				$array_rapkdet_unit, $array_rapkdet_price);
		}else{
			// --> subkon
			$array_detailpekerjaan=@$_POST["detailpekerjaan"];
			$array_detailpekerjaan=json_decode(stripslashes($array_detailpekerjaan));
			$array_parentcheck=@$_POST["parentcheck"];
			$array_parentcheck=json_decode(stripslashes($array_parentcheck));
			$result=$this->m_t_rapk->t_rapk_create_sub($rapk_total,$procurement,$branch, $project, $job, $array_detailpekerjaan, $array_parentcheck,$div_id );
		}
		echo $result;
		//echo 1;
	}
	
	
	//function for update record
	function t_rapk_update(){
		//POST variable here
		$rapk_id=@$_POST["rapk_id"];
		$rapk_id=is_numeric($rapk_id)?$rapk_id:0;
		$branch=@$_POST["branch"];
		$branch=is_numeric($branch)?$branch:0;
		$project=trim(@$_POST["project"]);
		$project=htmlentities($project,ENT_QUOTES);
						$rapk_total=@$_POST["rapk_total"];
			$rapk_total=htmlentities($rapk_total);
		$job=trim(@$_POST["job"]);
		$job=htmlentities($job,ENT_QUOTES);
		$div_id=@$_POST["div_id"];
		if ($div_id != '503. Sub Kontraktor'){
			//data detail
			$array_rapkdet_id=@$_POST["rapkdet_id"];
			$array_rapkdet_id=json_decode(stripslashes($array_rapkdet_id));
			$array_jobdet_id=@$_POST["jobdet_id"];
			$array_jobdet_id=json_decode(stripslashes($array_jobdet_id));
			$array_jobdet_name=@$_POST["jobdet_name"];
			$array_jobdet_name=json_decode(stripslashes($array_jobdet_name));
			$array_rapkdet_volume=@$_POST["rapkdet_volume"];
			$array_rapkdet_volume=json_decode(stripslashes($array_rapkdet_volume));
			$array_rapkdet_unit=@$_POST["rapkdet_unit"];
			$array_rapkdet_unit=json_decode(stripslashes($array_rapkdet_unit));
			$array_rapkdet_price=@$_POST["rapkdet_price"];
			$array_rapkdet_price=json_decode(stripslashes($array_rapkdet_price));

		
			$result = $this->m_t_rapk->t_rapk_update($rapk_total,$rapk_id, $branch, $project, $job, $array_rapkdet_id, $array_jobdet_id, $array_jobdet_name, $array_rapkdet_volume,
					$array_rapkdet_unit, $array_rapkdet_price);
		}else{
			$result = $this->m_t_rapk->t_rapk_update_sub($rapk_total,$rapk_id);
		}
		echo $result;
	}
	
	function t_rapkdet_detailtemp_delete(){
		$ids = @$_POST['ids']; // Get our array back and translate it :
		$pkid = json_decode(stripslashes($ids));
		$result=$this->m_t_rapk->t_rapkdet_detailtemp_delete($pkid);
		echo $result;
	}
	
	//function for delete selected record
	function t_rapk_delete(){
		$ids = @$_POST['ids']; // Get our array back and translate it :
		$pkid = json_decode(stripslashes($ids));
		$result=$this->m_t_rapk->t_rapk_delete($pkid);
		echo $result;
	}
	
	function t_rapkdet_grouptemp_truncate(){
		$by = @$_SESSION[SESSION_USERID];
		$result = $this->m_t_rapk->t_rapkdet_grouptemp_truncate($by);
		echo $result;
	}
	
	function t_rapkdet_grouptemp_create(){
		$group_id=@$_POST["group_id"];
		$group_id=is_numeric($group_id)?$group_id:0;
		$group_uraian=trim(@$_POST["group_uraian"]);
		$group_publish=(@$_POST["group_publish"]=='true'?1:0);
		$by = @$_SESSION[SESSION_USERID];
		if($by !== false){
			if ($group_id == 0 ){
				$result = $this->m_t_rapk->t_rapkdet_grouptemp_create($group_uraian, $group_publish, $by);
			}else{
				$result = $this->m_t_rapk->t_rapkdet_grouptemp_update($group_id,$group_uraian, $group_publish, $by);
			}
			echo 1;
		}else{
			echo 3;
		}
	}
	
	function t_rapkdet_grouptemp_delete(){
		$group_id=@$_POST["group_id"];
		$group_id=is_numeric($group_id)?$group_id:0;
		$by = @$_SESSION[SESSION_USERID];
		$result = $this->m_t_rapk->t_rapkdet_grouptemp_delete($group_id);
		echo $result;
	}
	
	function t_rapkdet_groupdettemp_create(){
		$groupdettemp_id=@$_POST["groupdettemp_id"];
		$groupdettemp_id=is_numeric($groupdettemp_id)?$groupdettemp_id:0;
		$group_id=@$_POST["group_id"];
		$group_id=is_numeric($group_id)?$group_id:0;
		$m_jobdet_id=@$_POST["m_jobdet_id"];
		$m_jobdet_id=is_numeric($m_jobdet_id)?$m_jobdet_id:0;
		$rapkdet_volume=@$_POST["rapkdet_volume"];
		$rapkdet_volume=is_numeric($rapkdet_volume)?$rapkdet_volume:0;

		$rapkdet_unit=@$_POST["rapkdet_unit"];
		$rapkdet_unit=is_numeric($rapkdet_unit)?$rapkdet_unit:0;
		$rapkdet_price=@$_POST["rapkdet_price"];
		$rapkdet_price=is_numeric($rapkdet_price)?$rapkdet_price:0;
		$rapkdet_publish=(@$_POST["rapkdet_publish"]=='true'?1:0);
		if ($groupdettemp_id == 0 ){
			$result = $this->m_t_rapk->t_rapkdet_groupdettemp_create($group_id, $m_jobdet_id, $rapkdet_volume, $rapkdet_unit, $rapkdet_price, $rapkdet_publish);
		}else{
			$result = $this->m_t_rapk->t_rapkdet_groupdettemp_update($groupdettemp_id,$group_id, $m_jobdet_id, $rapkdet_volume, $rapkdet_unit, $rapkdet_price, $rapkdet_publish);
		}
		echo 1;
	}

	//function for advanced search
	function t_rapk_search(){
		//POST varibale here
		$rapk_id=@$_POST["rapk_id"];
		$rapk_id=is_numeric($rapk_id)?$rapk_id:0;
		$rapk_flag=trim(@$_POST["rapk_flag"]);
		$rapk_flag=htmlentities($rapk_flag,ENT_QUOTES);
		$job=trim(@$_POST["job"]);
		$job=htmlentities($job,ENT_QUOTES);
		$project=trim(@$_POST["project"]);
		$project=htmlentities($project,ENT_QUOTES);
		$rapk_name=trim(@$_POST["rapk_name"]);
		$rapk_name=htmlentities($rapk_name,ENT_QUOTES);
		$rapk_shortname=trim(@$_POST["rapk_shortname"]);
		$rapk_shortname=htmlentities($rapk_shortname,ENT_QUOTES);
		$rapk_pm=trim(@$_POST["rapk_pm"]);
		$rapk_pm=htmlentities($rapk_pm,ENT_QUOTES);
		$rapk_semcm=trim(@$_POST["rapk_semcm"]);
		$rapk_semcm=htmlentities($rapk_semcm,ENT_QUOTES);
		$rapk_value=trim(@$_POST["rapk_value"]);
		$rapk_start=trim(@$_POST["rapk_start"]);
		$rapk_p1=trim(@$_POST["rapk_p1"]);
		$rapk_p2=trim(@$_POST["rapk_p2"]);
		$rapk_created_date=trim(@$_POST["rapk_created_date"]);
		$rapk_created_by=trim(@$_POST["rapk_created_by"]);
		$rapk_created_by=htmlentities($rapk_created_by,ENT_QUOTES);
		$rapk_updated_date=trim(@$_POST["rapk_updated_date"]);
		$rapk_updated_by=trim(@$_POST["rapk_updated_by"]);
		$rapk_updated_by=htmlentities($rapk_updated_by,ENT_QUOTES);
		$rapk_updated_count=@$_POST["rapk_updated_count"];
		$rapk_updated_count=is_numeric($rapk_updated_count)?$rapk_updated_count:0;
		$branch=@$_POST["branch_id"];
		$branch=is_numeric($branch)?$branch:0;
		
		$start = (integer) (isset($_POST['start']) ? $_POST['start'] : $_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['limit']);
		$result = $this->m_t_rapk->t_rapk_search($rapk_id ,$rapk_flag ,$job ,$project ,$rapk_name ,$rapk_shortname ,$rapk_pm ,$rapk_semcm ,$rapk_value ,$rapk_start ,$rapk_p1 ,$rapk_p2 ,$rapk_created_date ,$rapk_created_by ,$rapk_updated_date ,$rapk_updated_by ,$rapk_updated_count ,$branch ,$start,$end);
		echo $result;
	}


	function t_rapk_print(){
  		//POST varibale here
		$rapk_id=@$_POST["rapk_id"];
		$rapk_id=is_numeric($rapk_id)?$rapk_id:0;
		$rapk_flag=trim(@$_POST["rapk_flag"]);
		$rapk_flag=htmlentities($rapk_flag,ENT_QUOTES);
		$job=trim(@$_POST["job"]);
		$job=htmlentities($job,ENT_QUOTES);
		$project=trim(@$_POST["project"]);
		$project=htmlentities($project,ENT_QUOTES);
		$rapk_name=trim(@$_POST["rapk_name"]);
		$rapk_name=htmlentities($rapk_name,ENT_QUOTES);
		$rapk_shortname=trim(@$_POST["rapk_shortname"]);
		$rapk_shortname=htmlentities($rapk_shortname,ENT_QUOTES);
		$rapk_pm=trim(@$_POST["rapk_pm"]);
		$rapk_pm=htmlentities($rapk_pm,ENT_QUOTES);
		$rapk_semcm=trim(@$_POST["rapk_semcm"]);
		$rapk_semcm=htmlentities($rapk_semcm,ENT_QUOTES);
		$rapk_value=trim(@$_POST["rapk_value"]);
		$rapk_start=trim(@$_POST["rapk_start"]);
		$rapk_p1=trim(@$_POST["rapk_p1"]);
		$rapk_p2=trim(@$_POST["rapk_p2"]);
		$rapk_created_date=trim(@$_POST["rapk_created_date"]);
		$rapk_created_by=trim(@$_POST["rapk_created_by"]);
		$rapk_created_by=htmlentities($rapk_created_by,ENT_QUOTES);
		$rapk_updated_date=trim(@$_POST["rapk_updated_date"]);
		$rapk_updated_by=trim(@$_POST["rapk_updated_by"]);
		$rapk_updated_by=htmlentities($rapk_updated_by,ENT_QUOTES);
		$rapk_updated_count=@$_POST["rapk_updated_count"];
		$rapk_updated_count=is_numeric($rapk_updated_count)?$rapk_updated_count:0;
		$branch=@$_POST["branch_id"];
		$branch=is_numeric($branch)?$branch:0;
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		
		$data["data_print"] = $this->m_t_rapk->t_rapk_print($rapk_id ,$rapk_flag ,$job ,$project ,$rapk_name ,$rapk_shortname ,$rapk_pm ,$rapk_semcm ,$rapk_value ,$rapk_start ,$rapk_p1 ,$rapk_p2 ,$rapk_created_date ,$rapk_created_by ,$rapk_updated_date ,$rapk_updated_by ,$rapk_updated_count ,$branch ,$option,$filter);
		$print_view=$this->load->view("main/p_t_rapk.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/t_rapk_printlist.html","w+");
		fwrite($print_file, $print_view);
		echo '1';        
	}
	/* End Of Function */

	/* Function to Export Excel document */
	function t_rapk_export_excel(){
		//POST varibale here
		$rapk_id=@$_POST["rapk_id"];
		$rapk_id=is_numeric($rapk_id)?$rapk_id:0;
		$rapk_flag=trim(@$_POST["rapk_flag"]);
		$rapk_flag=htmlentities($rapk_flag,ENT_QUOTES);
		$job=trim(@$_POST["job"]);
		$job=htmlentities($job,ENT_QUOTES);
		$project=trim(@$_POST["project"]);
		$project=htmlentities($project,ENT_QUOTES);
		$rapk_name=trim(@$_POST["rapk_name"]);
		$rapk_name=htmlentities($rapk_name,ENT_QUOTES);
		$rapk_shortname=trim(@$_POST["rapk_shortname"]);
		$rapk_shortname=htmlentities($rapk_shortname,ENT_QUOTES);
		$rapk_pm=trim(@$_POST["rapk_pm"]);
		$rapk_pm=htmlentities($rapk_pm,ENT_QUOTES);
		$rapk_semcm=trim(@$_POST["rapk_semcm"]);
		$rapk_semcm=htmlentities($rapk_semcm,ENT_QUOTES);
		$rapk_value=trim(@$_POST["rapk_value"]);
		$rapk_start=trim(@$_POST["rapk_start"]);
		$rapk_p1=trim(@$_POST["rapk_p1"]);
		$rapk_p2=trim(@$_POST["rapk_p2"]);
		$rapk_created_date=trim(@$_POST["rapk_created_date"]);
		$rapk_created_by=trim(@$_POST["rapk_created_by"]);
		$rapk_created_by=htmlentities($rapk_created_by,ENT_QUOTES);
		$rapk_updated_date=trim(@$_POST["rapk_updated_date"]);
		$rapk_updated_by=trim(@$_POST["rapk_updated_by"]);
		$rapk_updated_by=htmlentities($rapk_updated_by,ENT_QUOTES);
		$rapk_updated_count=@$_POST["rapk_updated_count"];
		$rapk_updated_count=is_numeric($rapk_updated_count)?$rapk_updated_count:0;
		$branch=@$_POST["branch_id"];
		$branch=is_numeric($branch)?$branch:0;
		$option=$_POST['currentlisting'];
		$filter=$_POST["query"];
		$data["type"]="excel";
		$data["data_print"] = $this->m_t_rapk->t_rapk_print($rapk_id ,$rapk_flag ,$job ,$project ,$rapk_name ,$rapk_shortname ,$rapk_pm ,$rapk_semcm ,$rapk_value ,$rapk_start ,$rapk_p1 ,$rapk_p2 ,$rapk_created_date ,$rapk_created_by ,$rapk_updated_date ,$rapk_updated_by ,$rapk_updated_count ,$branch ,$option,$filter);
		$print_view=$this->load->view("main/p_t_rapk.php",$data,TRUE);
		if(!file_exists("print")){
			mkdir("print");
		}
		$print_file=fopen("print/t_rapk_printlist.xls","w+");
		fwrite($print_file, $print_view);
		echo '1';   
			
	}
	
	// Encodes a SQL array into a JSON formated string
	function JEncode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->encode($arr);  //encode the data in json format
		} else {
			$data = json_encode($arr);  //encode the data in json format
		}
		return $data;
	}
	
	// Decode a SQL array into a JSON formated string
	function JDecode($arr){
		if (version_compare(PHP_VERSION,"5.2","<"))
		{    
			require_once("./JSON.php"); //if php<5.2 need JSON class
			$json = new Services_JSON();//instantiate new json object
			$data=$json->decode($arr);  //decode the data in json format
		} else {
			$data = json_decode($arr);  //decode the data in json format
		}
		return $data;
	}
		
		function tes(){
			$this->m_t_rapk->tes();
		}
}
?>